package org.demo.solr;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrQuery.ORDER;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.FacetField.Count;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrInputDocument;
import org.demo.solr.bean.SolrBean;

/**
 * solrj 示例 不确保运行成功
 * @author litianyi
 *
 * 2015年11月10日
 */
public class Test {

	public static void main(String[] args) {

	}

	/**
	 * 添加
	 */
	public void add() {
		HttpSolrServer solrServer = SolrServer.getSolrServer("http://localhost:8080/solr/core0");
		SolrInputDocument doc = new SolrInputDocument();
		doc.addField("id", 1);
		doc.addField("tvName", "屌丝男士");

		try {
		
			solrServer.add(doc);
			solrServer.commit();

		} catch (SolrServerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 查询
	 * @throws SolrServerException
	 */
	public void query() throws SolrServerException {
		// 设置查询属性
		SolrQuery query = new SolrQuery();
//		query.setQuery("*:*");
		query.setQuery("tvName:屌丝男士");//这里不能用query.setQuery("*:*")过滤，否则就无法高亮
		// 系统基本过滤条件
		query.addFilterQuery("tvIsIntrest:0"); // 非片花
		query.setStart(0);
		query.setRows(10);
		query.setSort("createTime", ORDER.desc); // 更新时间倒序排
		
		boolean highlight = false;
		
		if (highlight) {
			query.setHighlight(true); // 开启高亮组件
			query.addHighlightField("tvName");// 高亮字段
			query.setHighlightSimplePre("<font color='yellow'>");// 标记，高亮关键字前缀
			query.setHighlightSimplePost("</font>");// 后缀
		}
		
		
		QueryResponse response = null;
		HttpSolrServer server = SolrServer.getSolrServer("http://localhost:8080/solr/core0");
		response = server.query(query);
		
		List<SolrBean> beans = response.getBeans(SolrBean.class);
	
		//设置高亮
		Map<String,Map<String,List<String>>> highlightMap=response.getHighlighting();
		for (SolrBean bean : beans) {
			System.out.println(highlightMap.get(bean.getId().toString()).get("tvName").get(0));
		}
	}
	
	/**
	 * 分组 demo
	 * @throws SolrServerException
	 */
	public void facet() throws SolrServerException{
		SolrQuery query = new SolrQuery();
		query.setQuery("*:*");
		query.setIncludeScore(false);// 是否按每组数量高低排序
		query.setFacet(true);// 是否分组查询
		query.setRows(0);// 设置返回结果条数，如果你是分组查询，你就设置为0
		query.addFacetField("createTime");// 增加分组字段
		query.setFacetMinCount(1).setFacetLimit(20);// 限制每次返回结果数
		HttpSolrServer server = SolrServer.getSolrServer("http://localhost:8080/solr/core0");

		QueryResponse rsp = server.query(query);

		List<Count> countList = rsp.getFacetField("createTime").getValues();

		Map<String, Integer> map = new LinkedHashMap<String, Integer>();
		if (countList != null) {
			for (Count count : countList) {
				System.out.println(count.getName().trim() + ":"
						+ count.getCount());
				// 分组可能会重复 要去重
				map.put(count.getName().trim(), (int) count.getCount());
			}
		}
	}
	
	/**
	 * 删除
	 * @throws SolrServerException
	 * @throws IOException
	 */
	public void delete() throws SolrServerException, IOException{
		HttpSolrServer server = SolrServer.getSolrServer("http://localhost:8080/solr/core0");
		server.deleteById("1");// 唯一索引
		// 删除查询到的索引信息
		// server.deleteByQuery("id:4 id:6");
		server.commit();

	}

}
